<?php
//Includes
require_once("MasterPage.php");
require_once("vcl/vcl.inc.php");
require_once("AdminLogin.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");
use_unit("rtl.inc.php");

//Class definition
class AdminDoRegister extends MasterPage
{
   public $Label2 = null;
   public $pnRegister = null;
   public $edVerifyPassword = null;
   public $lbVerifyPassword = null;
   public $edVerifyEmail = null;
   public $lbVerifyEmail = null;
   public $edRegistrationCode = null;
   public $lbRegistrationCode = null;
   public $bRegister = null;
   public $edPassword = null;
   public $lbPassword = null;
   public $edEmail = null;
   public $lbEmail = null;


   function bRegisterClick($sender, $params)
   {

      global $AdminLogin;
      global $DB;

      // TO-DO: Need to pattern match to ensure a good email was provided.
      if(strlen($this->edEmail->Text) < 7)
      {
         $this->lbMessage->Caption = 'You must provide a valid email address. Please try again.';
         return;
      }

      if(strlen($this->edPassword->Text) < 7)
      {
         $this->lbMessage->Caption = 'The password you select must be at least 7 characters long. Please try again.';
         return;
      }

      if($this->edEmail->Text != $this->edVerifyEmail->Text)
      {
         $this->lbMessage->Caption = 'The email addresses you provided must match. Please try again.';
         return;
      }

      if($this->edPassword->Text != $this->edVerifyPassword->Text)
      {
         $this->lbMessage->Caption = 'The passwords you provided must match. Please try again.';
         return;
      }

      // Check to see if the user has provided a valid registration code
      // TODO: This should be DB configurable
      if(strlen($this->edRegistrationCode->Text) == 0)
      {
         $this->lbMessage->Caption = 'A valid registration code must be provided.';
         return;
      }

      // Check to see if the user has provided a valid registration code
      // TODO: This should be DB configurable
      if($this->edRegistrationCode->Text != "mission")
      {
         $this->lbMessage->Caption = 'The registration code you provided is incorrect. Please try again.';
         return;
      }

      // Check to see if this email has already been used
      $DB->q_query->close();
      $DB->q_query->SQL = "SELECT * FROM person WHERE vchEmail = '" . mysql_real_escape_string($this->edEmail->Text) . "'";
      $DB->q_query->open();
      if($DB->q_query->RecordCount == 1)
      {
         $this->lbMessage->Caption = 'The email address specified is already registered.';
         $DB->q_query->close();
         return;
      }
      $DB->q_query->close();

      // Try to register using the specified username and password.
      if(!$AdminLogin->TryRegister($this->edEmail->Text, $this->edPassword->Text))
      {
         $this->lbMessage->Caption = 'An unknown registration error was encountered. Please contact jani@digitalelite.com for help!';
         return;
      }

      // Go ahead and log the user in with their newly established credentials
      $pid = $AdminLogin->TryLogin($this->edEmail->Text, $this->edPassword->Text);
      if($pid > 0)
      {
         $this->lbMessage->Caption = '';

         // Login succeeded, so redirect back to the main page.
         redirect("Org.php?action=edit&pid=" . $pid);

         // Redirect failed.
         $this->lbMessage->Caption = 'Registration succeeded but failed to redirect on login.';
      }
      else
      {
         $this->lbMessage->Caption = 'An unknown error has occurred.';
      }
   }
}

global $application;

global $AdminDoRegister;

//Creates the form
$AdminDoRegister = new AdminDoRegister($application);

//Read from resource file
$AdminDoRegister->loadResource(__FILE__);

//Shows the form
$AdminDoRegister->show();

?>